package de.dopichaj.labrador.inex.assess;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import de.dopichaj.labrador.inex.Assessment;

public final class XCGAssessor {

    private final double[] idealXCG;
    private final Assessment assessment;
    public XCGAssessor() {
        idealXCG = new double[1];
        idealXCG[0] = 1;
        this.assessment = null;
    }
    
    public XCGAssessor(List<? extends Assessable> ideal) {
        this(ideal, null);
    }
    
    public XCGAssessor(Assessment assessment) {
        this(assessment.getAssessableList(), assessment);
    }
    
    private XCGAssessor(List<? extends Assessable> ideal, Assessment assessment) {
        final List<Assessable> sortedIdeal = new ArrayList<Assessable>(ideal);
        Collections.sort(sortedIdeal, new Comparator<Assessable>() {

            public int compare(Assessable o1, Assessable o2) {
                return Double.compare(quant(o2), quant(o1));
            }});
        idealXCG = new double[ideal.size()];
        double lastXCG = 0;
        for (int i = 0; i < ideal.size(); i++) {
            lastXCG = idealXCG[i] = lastXCG + quant(sortedIdeal.get(i));
        }
        this.assessment = assessment;
    }
    
    public static Map<Integer, XCGAssessor> getAssessors(
        final Map<Integer, Assessment> assessmentMap) {
        
        final Map<Integer, XCGAssessor> result = new HashMap<Integer, XCGAssessor>();
        for (final Entry<Integer, Assessment> entry : assessmentMap.entrySet()) {
            
            result.put(entry.getKey(), new XCGAssessor(entry.getValue()));
        }
        
        return result;
    }
    
    public double assess(List<Assessable> result) {
        return assess(result, result.size());
    }
    
    public double assess(List<Assessable> result, int max) {
        double xCG = 0;
        final Iterator<Assessable> it = result.iterator();
        int pos = 0;
        while (pos < max && it.hasNext()) {
            final Assessable next = it.next();
            xCG += quant(next);
            pos++;
            assert xCG <= idealXCG(pos) : xCG + " > " + idealXCG(pos);
        }
        return xCG / idealXCG(max - 1);
    }
    
    public double[] assessUpTo(final List<Assessable> result, int max) {
        double[] xCG = new double[max];
        final Iterator<Assessable> it = result.iterator();
        int pos = 0;
        double lastXCG = 0;
        while (pos < max && it.hasNext()) {
            final Assessable next = it.next();
            lastXCG = xCG[pos] = lastXCG + quant(next);
            assert lastXCG <= idealXCG(pos) : lastXCG + " > " + idealXCG(pos);
            xCG[pos] /= idealXCG(pos);
            pos++;
        }
        while (pos < max) {
            xCG[pos] = lastXCG / idealXCG(pos);
            pos++;
        }
        return xCG;
    }

    private double idealXCG(int i) {
        if (i < idealXCG.length) {
            return idealXCG[i];
        } else {
            return idealXCG[idealXCG.length - 1];
        }
    }

    private double quant(final Assessable ass) {
        return ass.getExhaustivity() * ass.getSpecificity();
    }

    public double assessMeanAverage(List<? extends Assessable> result) {
        return assessMeanAverage(result.iterator(), result.size());
    }
    
    private double assessMeanAverage(Iterator<? extends Assessable> iterator, final int size) {
        double xCG = 0;
        double sum = 0;
        int pos = 0;
        while (pos < size && iterator.hasNext()) {
            final Assessable ass = iterator.next();
            xCG += quant(ass);
            sum += xCG / idealXCG(pos);
            pos++;
        }
        return sum / size;
    }
    
    public Assessable getAssessable(final String file, final String xPath) {
        return assessment.getAssessable(file, xPath);
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/